package com.tc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tc.domain.Hospital_reimbursement_ratio;
import com.tc.dto.Hospital_reimbursement_ratioQueryDTO;
import com.tc.mapper.Hospital_reimbursement_ratioMapper;
import com.tc.service.IHospital_reimbursement_ratioService;
import com.tc.vo.Hospital_reimbursement_ratioVO;
import com.tc.vo.PageResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 医院报销比例服务实现类
 * </p>
 *
 * @author jcd
 * @since 2025-07-09
 */
@Service
public class Hospital_reimbursement_ratioServiceImpl extends ServiceImpl<Hospital_reimbursement_ratioMapper, Hospital_reimbursement_ratio> implements IHospital_reimbursement_ratioService {

    /**
     * 分页查询医院报销比例信息
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @Override
    public PageResultVO<Hospital_reimbursement_ratioVO> pageQuery(Hospital_reimbursement_ratioQueryDTO queryDTO) {
        // 构建分页查询对象
        Page<Hospital_reimbursement_ratio> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        
        // 构建查询条件
        LambdaQueryWrapper<Hospital_reimbursement_ratio> queryWrapper = new LambdaQueryWrapper<>();
        
        // 显式添加未删除过滤条件
        queryWrapper.eq(Hospital_reimbursement_ratio::getDeleted, 0);
        
        // 添加查询条件
        if (StringUtils.hasText(queryDTO.getHospitalLevel())) {
            queryWrapper.eq(Hospital_reimbursement_ratio::getHospitalLevel, queryDTO.getHospitalLevel());
        }
        if (StringUtils.hasText(queryDTO.getPersonType())) {
            queryWrapper.eq(Hospital_reimbursement_ratio::getPersonType, queryDTO.getPersonType());
        }
        if (StringUtils.hasText(queryDTO.getStaus())) {
            queryWrapper.eq(Hospital_reimbursement_ratio::getStaus, queryDTO.getStaus());
        }
        
        // 报销比例区间查询
        if (queryDTO.getMinRatio() != null) {
            queryWrapper.ge(Hospital_reimbursement_ratio::getReimbursementRatio, queryDTO.getMinRatio());
        }
        if (queryDTO.getMaxRatio() != null) {
            queryWrapper.le(Hospital_reimbursement_ratio::getReimbursementRatio, queryDTO.getMaxRatio());
        }
        
        // 按更新时间倒序排序
        queryWrapper.orderByDesc(Hospital_reimbursement_ratio::getUpdateTime);
        
        // 执行分页查询
        IPage<Hospital_reimbursement_ratio> pageResult = this.page(page, queryWrapper);
        
        // 转换records
        List<Hospital_reimbursement_ratioVO> voList = new ArrayList<>();
        for (Hospital_reimbursement_ratio ratio : pageResult.getRecords()) {
            Hospital_reimbursement_ratioVO vo = new Hospital_reimbursement_ratioVO();
            BeanUtils.copyProperties(ratio, vo);
            voList.add(vo);
        }
        
        // 使用通用PageResultVO
        return PageResultVO.convert(pageResult, voList);
    }

    /**
     * 查询所有医院报销比例
     * @param status 状态，可选参数，传null表示查询所有状态
     * @return 医院报销比例列表
     */
    @Override
    public List<Hospital_reimbursement_ratioVO> listAll(String status) {
        // 构建查询条件
        LambdaQueryWrapper<Hospital_reimbursement_ratio> queryWrapper = new LambdaQueryWrapper<>();
        
        // 显式添加未删除过滤条件
        queryWrapper.eq(Hospital_reimbursement_ratio::getDeleted, 0);
        
        // 如果指定了状态，则添加状态条件
        if (StringUtils.hasText(status)) {
            queryWrapper.eq(Hospital_reimbursement_ratio::getStaus, status);
        }
        
        // 按医院等级和人员类型升序、更新时间降序排序
        queryWrapper.orderByAsc(Hospital_reimbursement_ratio::getHospitalLevel)
                .orderByAsc(Hospital_reimbursement_ratio::getPersonType)
                .orderByDesc(Hospital_reimbursement_ratio::getUpdateTime);
        
        // 查询全部
        List<Hospital_reimbursement_ratio> ratioList = this.list(queryWrapper);
        
        // 转换为VO对象
        List<Hospital_reimbursement_ratioVO> voList = new ArrayList<>();
        for (Hospital_reimbursement_ratio ratio : ratioList) {
            Hospital_reimbursement_ratioVO vo = new Hospital_reimbursement_ratioVO();
            BeanUtils.copyProperties(ratio, vo);
            voList.add(vo);
        }
        
        return voList;
    }
}
